package com.haixiaoke.saas.repository.service.impl;

import com.haixiaoke.saas.repository.mapper.ApartmentItemRuleRefMapper;
import com.haixiaoke.saas.repository.pojo.ApartmentChargeItem;
import com.haixiaoke.saas.repository.pojo.ApartmentItemRuleRef;
import com.haixiaoke.saas.repository.pojo.ApartmentOrderChargeItem;
import com.haixiaoke.saas.repository.service.IApartmentChargeItemService;
import com.haixiaoke.saas.repository.service.IApartmentItemRuleRefService;
import com.haixiaoke.saas.repository.service.IApartmentOrderChargeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 账单规则与收费项目关系Service业务层处理
 *
 * @author qixi
 * @date 2023-06-16
 */
@Service
public class ApartmentItemRuleRefServiceImpl implements IApartmentItemRuleRefService {
    @Autowired
    private ApartmentItemRuleRefMapper apartmentItemRuleRefMapper;
    @Autowired
    private IApartmentChargeItemService apartmentChargeItemService;
    @Autowired
    private IApartmentOrderChargeItemService apartmentOrderChargeItemService;

    /**
     * 查询账单规则与收费项目关系
     *
     * @param refId 账单规则与收费项目关系主键
     * @return 账单规则与收费项目关系
     */
    @Override
    public ApartmentItemRuleRef selectApartmentItemRuleRefByRefId(Long refId) {
        return apartmentItemRuleRefMapper.selectApartmentItemRuleRefByRefId(refId);
    }

    /**
     * 查询账单规则与收费项目关系列表
     *
     * @param apartmentItemRuleRef 账单规则与收费项目关系
     * @return 账单规则与收费项目关系
     */
    @Override
    public List<ApartmentItemRuleRef> selectApartmentItemRuleRefList(ApartmentItemRuleRef apartmentItemRuleRef) {
        return apartmentItemRuleRefMapper.selectApartmentItemRuleRefList(apartmentItemRuleRef);
    }

    /**
     * 新增账单规则与收费项目关系
     *
     * @param apartmentItemRuleRef 账单规则与收费项目关系
     * @return 结果
     */
    @Override
    public int insertApartmentItemRuleRef(ApartmentItemRuleRef apartmentItemRuleRef) {
        apartmentItemRuleRef.setCreateTime(new Date());
        return apartmentItemRuleRefMapper.insertApartmentItemRuleRef(apartmentItemRuleRef);
    }

    /**
     * 新增账单规则与收费项目关系
     *
     * @param apartmentItemRuleRef 账单规则与收费项目关系
     * @return 结果
     */
    @Override
    public int insertApartmentItemRuleRefAndChargeItem(ApartmentItemRuleRef apartmentItemRuleRef) {
        apartmentItemRuleRef.setCreateTime(new Date());
        Integer itemType = apartmentItemRuleRef.getItemType();

        ApartmentItemRuleRef ref = new ApartmentItemRuleRef();
        String itemName = apartmentItemRuleRef.getItemName();
        String itemUnit = apartmentItemRuleRef.getItemUnit();
        Double unitPrice = apartmentItemRuleRef.getUnitPrice();
        Long orgId = apartmentItemRuleRef.getOrgId();
        String userId = apartmentItemRuleRef.getUserId();
        String orderId = apartmentItemRuleRef.getOrderId();
        if (ApartmentItemRuleRef.ITEM_TYPE_DEFAULT_ITEM.equals(itemType)){
            ApartmentChargeItem item = new ApartmentChargeItem();
            item.setItemName(itemName);
            item.setItemUnit(itemUnit);
            item.setOrgId(orgId);
            item.setCreateBy(userId);
            item.setCreateTime(new Date());
            apartmentChargeItemService.insertApartmentChargeItem(item);

            ref.setRuleId(apartmentItemRuleRef.getRuleId());
            ref.setItemId(item.getItemId());
            ref.setItemType(itemType);
            ref.setItemName(itemName);
            ref.setItemUnit(itemUnit);
            ref.setUnitPrice(unitPrice);
            ref.setCreateBy(userId);
            ref.setCreateTime(new Date());
        }else {
            ApartmentOrderChargeItem orderChargeItem = new ApartmentOrderChargeItem();
            orderChargeItem.setItemName(itemName);
            orderChargeItem.setItemUnit(itemUnit);
            orderChargeItem.setOrderId(orderId);
            orderChargeItem.setCreateBy(userId);
            orderChargeItem.setCreateTime(new Date());
            apartmentOrderChargeItemService.insertApartmentOrderChargeItem(orderChargeItem);

            ref.setRuleId(apartmentItemRuleRef.getRuleId());
            ref.setItemId(orderChargeItem.getItemId());
            ref.setItemType(itemType);
            ref.setItemName(itemName);
            ref.setItemUnit(itemUnit);
            ref.setUnitPrice(unitPrice);
            ref.setCreateBy(userId);
            ref.setCreateTime(new Date());
        }
        return apartmentItemRuleRefMapper.insertApartmentItemRuleRef(ref);
    }

    /**
     * 修改账单规则与收费项目关系
     *
     * @param apartmentItemRuleRef 账单规则与收费项目关系
     * @return 结果
     */
    @Override
    public int updateApartmentItemRuleRef(ApartmentItemRuleRef apartmentItemRuleRef) {
        apartmentItemRuleRef.setUpdateTime(new Date());
        return apartmentItemRuleRefMapper.updateApartmentItemRuleRef(apartmentItemRuleRef);
    }

    /**
     * 批量删除账单规则与收费项目关系
     *
     * @param refIds 需要删除的账单规则与收费项目关系主键
     * @return 结果
     */
    @Override
    public int deleteApartmentItemRuleRefByRefIds(Long[] refIds) {
        return apartmentItemRuleRefMapper.deleteApartmentItemRuleRefByRefIds(refIds);
    }

    /**
     * 删除账单规则与收费项目关系信息
     *
     * @param refId 账单规则与收费项目关系主键
     * @return 结果
     */
    @Override
    public int deleteApartmentItemRuleRefByRefId(Long refId) {
        List<ApartmentItemRuleRef> refs = selectApartmentItemRuleRefByRuleId(refId);
        return apartmentItemRuleRefMapper.deleteApartmentItemRuleRefByRefId(refId);
    }

    @Override
    public List<ApartmentItemRuleRef> selectApartmentItemRuleRefByRuleId(Long ruleId) {
        return apartmentItemRuleRefMapper.selectApartmentItemRuleRefByRuleId(ruleId);
    }

    @Override
    public int deleteApartmentItemRuleRefNotInRefIds(Long ruleId ,List<Long> refIds) {
        return apartmentItemRuleRefMapper.deleteApartmentItemRuleRefNotInRefIds(ruleId,refIds);
    }

    @Override
    public boolean checkItemNamesUnique(List<String> itemNames,Long ruleId) {
        return apartmentItemRuleRefMapper.checkItemNamesUnique(itemNames,ruleId) == 0;
    }

    @Override
    public int insertApartmentItemRuleRefBatch(List<ApartmentItemRuleRef> refs) {
        return apartmentItemRuleRefMapper.insertApartmentItemRuleRefBatch(refs);
    }
}
